package org.itboys.seller.service;

import java.util.List;

import javax.annotation.Resource;

import org.itboys.commons.utils.collection.CommonCollectionUtils;
import org.itboys.commons.utils.collection.FetchCondition;
import org.itboys.mongodb.core.MongoDataSource;
import org.itboys.mongodb.service.BaseService;
import org.itboys.seller.entity.SellerMenu;
import org.springframework.stereotype.Service;

/**
 * 商家平台的菜单
 * @author weisky
 *
 */
@Service
public class SellerMenuService extends BaseService<SellerMenu, Long>{

	private static final long serialVersionUID = 1L;
	
	@Resource(name = "sellerDS")
	private MongoDataSource ds;
	
	@Override
	protected MongoDataSource getMongoDataSource() {
		return ds;
	}

	@Override
	protected Class<SellerMenu> getEntityClass() {
		return SellerMenu.class;
	}
	
	/**
	 * 保存或者修改
	 * @param sellerMenu
	 * @return
	 */
	public boolean insert(SellerMenu sellerMenu){
		try{
			if(sellerMenu.getId() != 0){
				update(sellerMenu);
			}else{
				save(sellerMenu);
			}
			return true;
		}catch(Exception e){
			e.printStackTrace();
			logger.error("SellerMenuService.insert error", e);
			return false;
		}
	}
	
	/**
	 * 删除
	 * @param sellerMenuId
	 * @return
	 */
	public boolean dele(Long sellerMenuId){
		try{
			delete(sellerMenuId);
			return true;
		}catch(Exception e){
			e.printStackTrace();
			logger.error("SellerMenuService.dele", e);
			return false;
		}
	}
	
	/**
	 * 获取所有一级菜单 忽略权限
	 * 
	 * @param menus
	 * @return
	 */
	public List<SellerMenu> getRootsMenusIgnorePermission(List<SellerMenu> menus) {
		return CommonCollectionUtils.filterCollection(menus,
				new FetchCondition<SellerMenu>() {
					@Override
					public boolean needFetch(SellerMenu t) {
						return t.getParentId() == 0L;
					}
				});
	}
}
